if(CMAKE_TOOLCHAIN_FILE)
    set(LIBRARY_OUTPUT_PATH_ROOT ${CMAKE_BINARY_DIR} CACHE PATH "root for library output, set this to change where android libs are compiled to")
    # get absolute path, but get_filename_component ABSOLUTE only refer with source dir, so find_file here :(
    get_filename_component(CMAKE_TOOLCHAIN_FILE_NAME ${CMAKE_TOOLCHAIN_FILE} NAME)
    find_file(CMAKE_TOOLCHAIN_FILE ${CMAKE_TOOLCHAIN_FILE_NAME} PATHS ${CMAKE_SOURCE_DIR} NO_DEFAULT_PATH)
    message(STATUS "CMAKE_TOOLCHAIN_FILE = ${CMAKE_TOOLCHAIN_FILE}")
endif()

if(NOT DEFINED CMAKE_INSTALL_PREFIX)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
endif()
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")

if(NOT DEFINED NCNN_VERSION)
    string(TIMESTAMP NCNN_VERSION "%Y%m%d")
endif()

set(NCNN_VERSION_MAJOR 1)
set(NCNN_VERSION_MINOR 0)
set(NCNN_VERSION_PATCH ${NCNN_VERSION})
set(NCNN_VERSION_STRING ${NCNN_VERSION_MAJOR}.${NCNN_VERSION_MINOR}.${NCNN_VERSION_PATCH})
if(APPLE OR IOS)
    # macos / ios only accepts a.b.c.d.e where a=24bit b/c/d/e=10bit
    # 20201228 to 20.12.28
    string(SUBSTRING ${NCNN_VERSION} 2 2 NCNN_VERSION_YEAR)
    string(SUBSTRING ${NCNN_VERSION} 4 2 NCNN_VERSION_MONTH)
    string(SUBSTRING ${NCNN_VERSION} 6 2 NCNN_VERSION_DAY)
    set(NCNN_VERSION_STRING ${NCNN_VERSION_MAJOR}.${NCNN_VERSION_MINOR}.${NCNN_VERSION_YEAR}.${NCNN_VERSION_MONTH}.${NCNN_VERSION_DAY})
endif()
message(STATUS "NCNN_VERSION_STRING = ${NCNN_VERSION_STRING}")

cmake_minimum_required(VERSION 2.8.12)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE release CACHE STRING "Choose the type of build" FORCE)
endif()

if(NOT CMAKE_VERSION VERSION_LESS "3.15")
    # enable CMAKE_MSVC_RUNTIME_LIBRARY
    cmake_policy(SET CMP0091 NEW)
endif()

if(POLICY CMP0025)
    # reference from https://cmake.org/cmake/help/latest/policy/CMP0025.html
    cmake_policy(SET CMP0025 NEW)
endif()

project(ncnn)

if(MSVC AND NOT CMAKE_VERSION VERSION_LESS "3.15")
    option(NCNN_BUILD_WITH_STATIC_CRT "Enables use of statically linked CRT for statically linked ncnn" OFF)
    if(NCNN_BUILD_WITH_STATIC_CRT)
        # cmake before version 3.15 not work
        set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
    endif()
endif()

option(NCNN_SHARED_LIB "shared library support" OFF)
option(NCNN_ENABLE_LTO "enable link-time optimization" OFF)
option(NCNN_OPENMP "openmp support" ON)
option(NCNN_STDIO "load model from external file" ON)
option(NCNN_STRING "plain and verbose string" ON)
option(NCNN_INSTALL_SDK "install ncnn library and headers" ON)
option(NCNN_SIMPLEOCV "minimal opencv structure emulation" OFF)
option(NCNN_SIMPLEOMP "minimal openmp runtime emulation" OFF)
option(NCNN_SIMPLESTL "minimal cpp stl structure emulation" OFF)
option(NCNN_THREADS "build with threads" ON)
option(NCNN_BENCHMARK "print benchmark information for every layer" OFF)
option(NCNN_PLATFORM_API "build with platform api candy" ON)
option(NCNN_PIXEL "convert and resize from/to image pixel" ON)
option(NCNN_PIXEL_ROTATE "rotate image pixel orientation" ON)
option(NCNN_PIXEL_AFFINE "warp affine image pixel" ON)
option(NCNN_PIXEL_DRAWING "draw basic figure and text" ON)
option(NCNN_CMAKE_VERBOSE "print verbose cmake messages" OFF)
option(NCNN_VULKAN "vulkan compute support" OFF)
option(NCNN_SYSTEM_GLSLANG "use system glslang library" OFF)
option(NCNN_RUNTIME_CPU "runtime dispatch cpu routines" ON)
option(NCNN_DISABLE_PIC "disable position-independent code" OFF)
option(NCNN_BUILD_TESTS "build tests" OFF)
option(NCNN_COVERAGE "build for coverage" OFF)
option(NCNN_BUILD_BENCHMARK "build benchmark" ON)
option(NCNN_PYTHON "build python api" OFF)
option(NCNN_INT8 "int8 inference" ON)
option(NCNN_BF16 "bf16 inference" ON)

if(ANDROID OR IOS OR NCNN_SIMPLESTL OR CMAKE_CROSSCOMPILING)
    option(NCNN_DISABLE_RTTI "disable rtti" ON)
    option(NCNN_BUILD_TOOLS "build tools" OFF)
    option(NCNN_BUILD_EXAMPLES "build examples" OFF)
else()
    option(NCNN_DISABLE_RTTI "disable rtti" OFF)
    option(NCNN_BUILD_TOOLS "build tools" ON)
    option(NCNN_BUILD_EXAMPLES "build examples" ON)
endif()

if(ANDROID OR IOS OR LINUX OR NCNN_SIMPLESTL)
    option(NCNN_DISABLE_EXCEPTION "disable exception" ON)
else()
    option(NCNN_DISABLE_EXCEPTION "disable exception" OFF)
endif()

if(NCNN_SHARED_LIB)
    if(NCNN_BUILD_TESTS)
        message(WARNING "NCNN_SHARED_LIB must be OFF to build tests! NCNN_BUILD_TESTS will be turned off.")
        set(NCNN_BUILD_TESTS OFF)
    endif()

    if(NCNN_ENABLE_LTO)
        # enable global link time optimization
        cmake_policy(SET CMP0069 NEW)
        set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
        include(CheckIPOSupported)
        check_ipo_supported(RESULT ipo_supported OUTPUT ipo_supported_output)
        if(ipo_supported)
            set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
        else()
            message(WARNING "IPO is not supported: ${ipo_supported_output}")
            set(NCNN_ENABLE_LTO OFF)
        endif()
    endif()
endif()

if(NOT NCNN_STDIO OR NOT NCNN_STRING)
    if(NCNN_BUILD_TOOLS)
        message(WARNING "NCNN_STDIO or NCNN_STRING disabled, NCNN_BUILD_TOOLS will be turned off.")
        set(NCNN_BUILD_TOOLS OFF)
    endif()
    if(NCNN_BUILD_EXAMPLES)
        message(WARNING "NCNN_STDIO or NCNN_STRING disabled, NCNN_BUILD_EXAMPLES will be turned off.")
        set(NCNN_BUILD_EXAMPLES OFF)
    endif()
    if(NCNN_BUILD_BENCHMARK)
        message(WARNING "NCNN_STDIO or NCNN_STRING disabled, NCNN_BUILD_BENCHMARK will be turned off.")
        set(NCNN_BUILD_BENCHMARK OFF)
    endif()
    if(NCNN_BUILD_TESTS)
        message(WARNING "NCNN_STDIO or NCNN_STRING disabled, NCNN_BUILD_TESTS will be turned off.")
        set(NCNN_BUILD_TESTS OFF)
    endif()
endif()

##############################################

if((IOS AND CMAKE_OSX_ARCHITECTURES MATCHES "arm")
    OR (APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES "arm64")
    OR (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|aarch64)"))
    set(NCNN_TARGET_ARCH arm)

    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag("-march=armv8.2-a+fp16" NCNN_COMPILER_SUPPORT_ARM82_FP16)
    check_cxx_compiler_flag("-march=armv8.2-a+fp16+dotprod" NCNN_COMPILER_SUPPORT_ARM82_FP16_DOTPROD)

    if(NCNN_COMPILER_SUPPORT_ARM82_FP16)
        option(NCNN_ARM82 "optimize aarch64 platform with armv8.2" ON)
        if(NCNN_COMPILER_SUPPORT_ARM82_FP16_DOTPROD)
            if(NCNN_ARM82)
                option(NCNN_ARM82DOT "optimize aarch64 platform with armv8.2 dotprod" ON)
            endif()
        else()
            message(WARNING "The compiler does not support armv8.2 dotprod. NCNN_ARM82DOT will be OFF.")
        endif()
    else()
        message(WARNING "The compiler does not support armv8.2. NCNN_ARM82 will be OFF.")
    endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(mips)")
    set(NCNN_TARGET_ARCH mips)

    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag("-mmsa" NCNN_COMPILER_SUPPORT_MIPS_MSA)
    check_cxx_compiler_flag("-mloongson-mmi" NCNN_COMPILER_SUPPORT_LOONGSON_MMI)

    if(NCNN_COMPILER_SUPPORT_MIPS_MSA)
        option(NCNN_MSA "optimize mips platform with msa extension" ON)
        if(NCNN_COMPILER_SUPPORT_LOONGSON_MMI)
            if(NCNN_MSA)
                option(NCNN_MMI "optimize aarch64 platform with loongson mmi extension" ON)
            endif()
        else()
            message(WARNING "The compiler does not support loongson mmi extension. NCNN_MMI will be OFF.")
        endif()
    else()
        message(WARNING "The compiler does not support msa extension. NCNN_MSA will be OFF.")
    endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv)")
    set(NCNN_TARGET_ARCH riscv)

    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag("-march=rv64gcv" NCNN_COMPILER_SUPPORT_RVV)
    check_cxx_compiler_flag("-march=rv64gcv_zfh" NCNN_COMPILER_SUPPORT_RVV_FP16)

    if(NCNN_COMPILER_SUPPORT_RVV)
        option(NCNN_RVV "optimize risc-v platform with v extension" ON)
        if(NOT NCNN_COMPILER_SUPPORT_RVV_FP16)
            message(WARNING "The compiler does not support risc-v zfh extension. Upgrading your toolchain is strongly recommended.")
        endif()
    else()
        message(WARNING "The compiler does not support risc-v v extension. NCNN_RVV will be OFF.")
    endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc)")
    set(NCNN_TARGET_ARCH powerpc)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(loongarch)")
    set(NCNN_TARGET_ARCH loongarch)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(xtensa)")
    set(NCNN_TARGET_ARCH xtensa)
else()
    set(NCNN_TARGET_ARCH x86)
    option(NCNN_SSE2 "optimize x86 platform with sse2" ON)
    if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NOT (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7))
        option(NCNN_AVX2 "optimize x86 platform with avx2" ON)
        option(NCNN_AVX "optimize x86 platform with avx" ON)
    endif()
endif()

message(STATUS "Target arch: ${NCNN_TARGET_ARCH}")

##############################################

# set cmake default folder name
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "cmake")

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=256MB -s EXIT_RUNTIME=1")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=256MB -s EXIT_RUNTIME=1")
    set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=256MB -s EXIT_RUNTIME=1")

    if(NCNN_OPENMP AND NCNN_SIMPLEOMP)
        # TODO better flags for emscripten
        # node --experimental-wasm-threads xxx.js
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=15")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=15")
        set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=15")
    endif()
endif()

if(NCNN_VULKAN)
    if(NCNN_SYSTEM_GLSLANG)
        set(GLSLANG_TARGET_DIR "GLSLANG-NOTFOUND" CACHE PATH "Absolute path to glslangTargets.cmake directory")
        if(NOT GLSLANG_TARGET_DIR AND NOT DEFINED ENV{GLSLANG_TARGET_DIR})
            message(WARNING "GLSLANG_TARGET_DIR must be defined! NCNN_SYSTEM_GLSLANG will be turned off.")
            set(NCNN_SYSTEM_GLSLANG OFF)
        else()
            message(STATUS "Using glslang install located at ${GLSLANG_TARGET_DIR}")

            find_package(Threads)

            include("${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake")
            include("${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake")
            if(EXISTS "${GLSLANG_TARGET_DIR}/HLSLTargets.cmake")
                # hlsl support can be optional
                include("${GLSLANG_TARGET_DIR}/HLSLTargets.cmake")
            endif()
            include("${GLSLANG_TARGET_DIR}/glslangTargets.cmake")
            include("${GLSLANG_TARGET_DIR}/SPIRVTargets.cmake")

            if (NOT TARGET glslang OR NOT TARGET SPIRV)
                message(WARNING "glslang or SPIRV target not found! NCNN_SYSTEM_GLSLANG will be turned off.")
                set(NCNN_SYSTEM_GLSLANG OFF)
            endif()
        endif()
    endif()

    if(NOT NCNN_SYSTEM_GLSLANG)
        if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/glslang/CMakeLists.txt")
            message(FATAL_ERROR "The submodules were not downloaded! Please update submodules with \"git submodule update --init\" and try again.")
        else()
            # glslang requires c++11
            set(CMAKE_CXX_STANDARD 11)

            option(BUILD_EXTERNAL "" OFF)
            option(ENABLE_SPVREMAPPER "" OFF)
            option(ENABLE_GLSLANG_BINARIES "" OFF)
            option(ENABLE_HLSL "" OFF)
            option(ENABLE_RTTI "" OFF)
            option(ENABLE_EXCEPTIONS "" OFF)
            option(ENABLE_OPT "" OFF)
            option(ENABLE_PCH "" OFF)
            option(ENABLE_CTEST "" OFF)
            if(NCNN_SHARED_LIB)
                option(SKIP_GLSLANG_INSTALL "" ON)
            endif()
            add_subdirectory(glslang)
            if(NCNN_SHARED_LIB)
                if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_CXX_COMPILER_FRONTEND_VARIANT MATCHES "MSVC"))
                    target_compile_options(glslang PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
                    target_compile_options(OGLCompiler PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
                    target_compile_options(OSDependent PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
                    target_compile_options(SPIRV PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
                endif()
                if(NCNN_ENABLE_LTO)
                    set_target_properties(glslang PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
                    set_target_properties(OGLCompiler PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
                    set_target_properties(OSDependent PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
                    set_target_properties(SPIRV PROPERTIES INTERPROCEDURAL_OPTIMIZATION ON)
                endif()
            endif()
        endif()
    endif()
endif()

add_subdirectory(src)
if(NCNN_BUILD_BENCHMARK)
    add_subdirectory(benchmark)
endif()
if(NCNN_BUILD_EXAMPLES)
    add_subdirectory(examples)
endif()
if(NCNN_BUILD_TOOLS)
    add_subdirectory(tools)
endif()
if(NCNN_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()
if(NCNN_PYTHON)
    add_subdirectory(python)
endif()
